package database.creation.example;

import database.creation.example.R;
import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.widget.SimpleCursorAdapter;

public class ListadoActivity extends ListActivity {
	private Cursor cursor;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		/*
		 * Query a la base de datos
		 */
		ComunidadesSQLHelper dbHelper = new ComunidadesSQLHelper(ListadoActivity.this);

		SQLiteDatabase db = dbHelper.getWritableDatabase();

		String[] camposDb = { "comunidad", BaseColumns._ID  };
		cursor = db.query("comunidades", camposDb, null, null, null, null, "comunidad ASC");
		
		
		/*
		 * Mapeamos las querys SQL a los campos de las vistas
		 */
		int[] camposView = new int[] { android.R.id.text1 };

		SimpleCursorAdapter adapter = new SimpleCursorAdapter(ListadoActivity.this,
				android.R.layout.simple_list_item_1, cursor, camposDb,
				camposView);

		setListAdapter(adapter);
	}

	@Override
	protected void onStart() {
		super.onStart();
		startManagingCursor(cursor);
	}

	@Override
	protected void onStop() {
		stopManagingCursor(cursor);
		super.onStop();
	}

}